#!/bin/bash
#
# Update generated stuff in graphics/ from the source files.
#
# It would be nice to do this with waf but:
# a) getting the wscript right is a complete pain in the arse
# b) running Inkscape from the command line on OS X is a complete pain in the arse

pwd=`pwd`
if [ `basename $pwd` != "graphics" ]; then
    echo "$0: run within graphics/"
    exit 1
fi

if [ `uname -s` == "Darwin" ]; then
    # Convert OS X icons using OS X-only iconutil
    for p in dcpomatic2 dcpomatic2_kdm dcpomatic2_server dcpomatic2_batch dcpomatic2_player dcpomatic2_playlist; do
	iconutil --convert icns --output osx/$p.icns osx/$p.iconset
    done
else

    for c in icotool convert inkscape; do
        if [ ! -x "$(command -v $c)" ]; then
            echo "$c is required"
            exit 1
        fi
    done

    INKSCAPE="inkscape -z -C -e"

    # OS X application icons
    mkdir -p osx
    for r in 16 32 128 256 512; do
	for p in dcpomatic2; do
	    mkdir -p osx/$p.iconset
	    convert src/dcpomatic.png -resize $rx$r osx/$p.iconset/icon_${r}x${r}.png
	    convert src/dcpomatic.png -resize $rx$r osx/$p.iconset/icon_${r}x${r}@2x.png
	done
	for p in dcpomatic2_kdm dcpomatic2_server dcpomatic2_batch dcpomatic2_player dcpomatic2_playlist; do
	    mkdir -p osx/$p.iconset
	    $INKSCAPE osx/$p.iconset/icon_${r}x${r}.png -w $r -h $r src/$p.svg
	    $INKSCAPE osx/$p.iconset/icon_${r}x${r}@2x.png -w $r -h $r src/$p.svg
	done
    done

    # Linux application icons
    mkdir -p linux
    for r in 16 22 32 48 64 128 256 512; do
	mkdir -p linux/$r
	convert src/dcpomatic.png -resize $rx$r linux/$r/dcpomatic2.png
	for p in dcpomatic2_kdm dcpomatic2_server dcpomatic2_batch dcpomatic2_player dcpomatic2_playlist; do
	    $INKSCAPE linux/$r/$p.png src/$p.svg -w $r -h $r
	done
    done

    # Windows application icons
    mkdir -p windows
    for p in dcpomatic2 dcpomatic2_kdm dcpomatic2_server dcpomatic2_batch dcpomatic2_player dcpomatic2_playlist; do
	icotool -c -o windows/$p.ico linux/16/$p.png linux/32/$p.png linux/48/$p.png linux/64/$p.png linux/128/$p.png
    done
    convert src/dcpomatic.png -resize 400x400 windows/dcpomatic.bmp

    # OS X preferences icons
    # servers.png does not have an SVG version
    mkdir -p osx/preferences
    for i in colour_conversions defaults email kdm_email cover_sheet keys tms notifications accounts locations; do
	$INKSCAPE osx/preferences/$i.png src/$i.svg -w 32 -h 32
    done

    # OS X menu bar icon for the server
    $INKSCAPE osx/dcpomatic_small.png -w 64 -h 64 src/dcpomatic_small.svg

    # Splash screen (all platforms)
    $INKSCAPE splash.png src/splash.svg -w 400 -h 300

    # Timeline toolbar icons (all platforms)
    for i in select zoom zoom_all snap sequence; do
        $INKSCAPE $i.png src/$i.svg -w 32 -h 32
    done

    # Playlist editor tick/no-tick
    $INKSCAPE tick.png src/tick.svg -w 16 -h 16
    $INKSCAPE no_tick.png src/no_tick.svg -w 16 -h 16

    # favicon
    mkdir -p web
    convert src/web.png -resize 256x256 -transparent white web/favicon-256x256.png
    for r in 16 32 64 128; do
	convert web/favicon-256x256.png -resize $rx$r web/favicon-${r}x${r}.png
    done
    convert web/favicon-16x16.png web/favicon-32x32.png web/favicon-64x64.png web/favicon-128x128.png web/favicon-256x256.png web/favicon.ico
    convert src/web.png -resize 123x123 -transparent white web/logo.png
fi
